package me.zingon.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import me.zingon.backup.util.Maps;
import me.zingon.model.Field;

public class OracleTableDao implements ITableDao {
	
	public void loadTables(){
		String sql="select table_name from user_tables";
        PreparedStatement ps=null;
        try {
            ps=DBcon.getCon().prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Maps.put(rs.getString(1).toLowerCase(),loadFields(rs.getString(1)));
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
	}
	
	public List<Field> loadFields(String table){
        String sql=
        		"Select Tab.Column_Name    As Columnname," +
        				"       Tab.Data_Type      As Columntype," + 
//        				"       Tab.Data_Length    As Columnlength," + 
//        				"       Tab.Data_Precision As Columnprecision," + 
//        				"       Tab.Data_Scale     As Columnscale," + 
        				"       Com.Comments       As Columncomment" + 
        				"  From User_Tab_Columns Tab" + 
        				" Inner Join User_Col_Comments Com" + 
        				"    On Tab.Column_Name = Com.Column_Name" + 
        				" Where Tab.Table_Name = Com.Table_Name" + 
        				"   And Tab.Table_Name = '"+ table + "'"; 
        PreparedStatement ps=null;
        List<Field> fields=new ArrayList<Field>();
        try {
            ps=DBcon.getCon().prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            while(rs.next()){
                Field field=new Field();
                field.setField(rs.getString(1).toLowerCase());
                field.setType(rs.getString(2).toLowerCase());
//                field.setCollation(rs.getString(3));
//                field.setNvll(rs.getString(4));
//                field.setKey(rs.getString(5));
//                field.setDefaults(rs.getString(6));
//                field.setExtra(rs.getString(7));
//                field.setPrivileges(rs.getString(8));
                field.setComment(rs.getString(3).toLowerCase());
                fields.add(field);
            }
            rs.close();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fields;

	}

}
